<template>
  <div class="login_container" >
    <!-- 标题 -->
    <div class="login_title">
      <img src="../assets/images/title.png" alt="">
      <p>智慧工地管理系统</p>
    </div>
    <div class="login_box">
      <!-- 登录说明 -->
      <div class="login_descriptions">
        <p class="p1">用户登录</p>
        <p class="p2">USER LOGGIN</p>
        <div class="hover-container">
          <span class="hover-text">登录说明</span>
          <span class="tooltip-text">默认：用户名：admin 密码：123456</span>
        </div>
      </div>
      <!-- 左边图片  -->
      <div class="user-password">
        <img src="../assets/images/user.png" class="img1">
        <img src="../assets/images/password.png" class="img2">
      </div>

      <div class="login_form">
        <el-form :model="user" :rules="rules" ref="loginRef">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="user.username" class="input1"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password  placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>

          <el-form-item prop="code">
            <div style="display: flex">
              <el-input placeholder="请输入验证码" prefix-icon="el-icon-circle-check" size="medium" style="flex: 1" v-model="user.code"></el-input>
              <div style="flex: 1; height: 36px">
                <valid-code @update:value="getCode" />
              </div>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登 录</el-button>
          </el-form-item>
        </el-form>

        <div style="display: flex">
          <div style="flex: 1"><span style="color: #0f9876; cursor: pointer" @click="$router.push('/register')">注册</span></div>
          <div style="flex: 1; text-align: right"><span style="color: #0f9876; cursor: pointer">忘记密码</span></div>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode";
export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {

    // 验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }

    return {
      code: '',  // 验证码组件传递过来的code
      user:{
        code: '',   // 表单里用户输入的code 验证码
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          //{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code: [
          { validator: validateCode, trigger: 'blur' }
        ],
      }
    }
  },

  created() {
  },
  methods: {
    getCode(code) {
      this.code = code.toLowerCase()
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
        console.log(this.user);
          this.$axios.post(this.$httpUrl+'/login', this.user).then(res => {
            console.log(res)
            if (res.data.code == 200) {
              this.$root.$loginFlag = true;
              this.$router.push('/systemHome')
              this.$message.success('登录成功')
              localStorage.setItem("honey-user", JSON.stringify(res.data))  // 存储用户数据
            } else {
              this.$message.error('账号或密码错误')
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>
.login_container {
  background-image: url('@/assets/images/bcg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: absolute;
  width: 1370px;
  height: 800px;
  padding: 0px;
  top: 0;
  left: 0;
}
.login_title{
  width: 479px;
  height: 50px;
  font-size:40px;
  color: aqua;
  position: relative;
  left: 35%;
  top: 15%;
}
.login_title img{
  float: left;
  width: 50px;
  height: auto;
}
.login_title p{
  width: 420px;
  height: 45px;
  text-align: center;
}

.login_box {
  width: 479px;
  height: 521px;
  background: rgb(255, 255, 255);
  z-index: 0;
  border-radius: 3px;
  box-shadow: 0 0 10px ;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}


.login_descriptions{
  width: 479px;
  height: 50px;
  color: aqua;
  position: relative;
  top: 5%;
}
.p1{
  font-size: 30px;
  position: relative;
  left: 5%;
  display: inline;
}
.p2{
  font-size: 17px;
  position: relative;
  left: 8%;
  display: inline;
}
.hover-container {
  font-size: 15px;
  color: darkgray;
  position: relative;
  left: 35%;
  display: inline;

}
.tooltip-text {
  position: absolute;
  z-index: 1;
  visibility: hidden;
  white-space: nowrap;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s;
}
.hover-container:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}

.user-password{
  float: left;
  width: 50px;
  height: 200px;
  position: relative;
  top: 20%;
  left: 5%;
}
.img1{
  width: 50px;
  height: 50px;
  margin-bottom: 30px;
}
.img2{
  width: 50px;
  height: 50px;
}


.login_form {
  float: left;
  width: 300px;
  height: 350px;
  position: relative;
  top: 20%;
  left: 10%;

}
.input1{
  width: 300px;
  height: 50px;
  border-radius: 40% ;
  margin-bottom: 20px;
}

</style>